package com.gitee.wsl.android.ui.theme.inject

import android.app.Activity
import android.app.Application
import android.content.Context
import android.os.Bundle
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.LayoutInflaterCompat

import com.gitee.wsl.common.ui.theme.ColorPaletteScheme
import com.gitee.wsl.common.ui.theme.Theme
import com.gitee.wsl.common.ui.theme.TypographyScheme
import com.gitee.wsl.android.ext.ApplicationInit
import com.gitee.wsl.android.ui.ext.ApplicationScope
import timber.log.Timber

fun <P: ColorPaletteScheme,T: TypographyScheme,THEME: Theme<P, T>> ApplicationScope.autoInjectViewTheme(theme:THEME){
    ApplicationInit.application.registerActivityLifecycleCallbacks(
        object : Application.ActivityLifecycleCallbacks {
            override fun onActivityPreCreated(activity: Activity, savedInstanceState: Bundle?) {
                LayoutInflaterCompat.setFactory2(
                    activity.layoutInflater,
                    object : LayoutInflater.Factory2 {
                        override fun onCreateView(
                            parent: View?,
                            name: String,
                            context: Context,
                            attrs: AttributeSet
                        ): View? {

                            val createView= (activity as? AppCompatActivity)?.delegate?.createView(
                                parent,
                                name,
                                context,
                                attrs
                            )
                            createView?.let {view->
                                Timber.d("create $view")
                                if(!MaterialViewThemeInject.injectTheme(theme, view))
                                    if(!AndroidXViewThemeInject.injectTheme(theme, view))
                                        AndroidViewThemeInject.injectTheme(theme, view)
                            }
                            return createView
                        }

                        override fun onCreateView(
                            name: String,
                            context: Context,
                            attrs: AttributeSet
                        ): View? {
                            return null
                        }
                    })
            }

            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
                TODO("Not yet implemented")
            }

            override fun onActivityStarted(activity: Activity) {
                TODO("Not yet implemented")
            }

            override fun onActivityResumed(activity: Activity) {
                TODO("Not yet implemented")
            }

            override fun onActivityPaused(activity: Activity) {
                TODO("Not yet implemented")
            }

            override fun onActivityStopped(activity: Activity) {
                TODO("Not yet implemented")
            }

            override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
                TODO("Not yet implemented")
            }

            override fun onActivityDestroyed(activity: Activity) {
                TODO("Not yet implemented")
            }
        }
    )
}
